Apache Commons IO লাইব্রেরি Reader এবং Writer ক্লাসের ব্যবস্থাপনায় সহায়তা করে, যা Java I/O API এর উপরে নির্ভর করে। এই লাইব্রেরি InputStreamReader, OutputStreamWriter, এবং BufferedReader, BufferedWriter এর মতো সাধারণ Java ক্লাসের কার্যকারিতা বাড়িয়ে তোলে। এর মাধ্যমে আপনি আরও সহজ এবং কার্যকরভাবে টেক্সট ফাইল পড়া ও লেখা করতে পারেন।
এখানে, আমরা Reader এবং Writer ব্যবস্থাপনা নিয়ে আলোচনা করব এবং Apache Commons IO লাইব্রেরি ব্যবহার করে কিভাবে এগুলো আরও সহজভাবে ব্যবহার করা যায় তা দেখব।
BufferedReader এবং BufferedWriter দুটি জনপ্রিয় ক্লাস যা Java এর Reader এবং Writer ইন্টারফেসের উপরে কাজ করে। এগুলি স্ট্রিমের মাধ্যমে ফাইল বা ডেটা রিড এবং রাইট করার সময় পারফরম্যান্স উন্নত করতে সহায়তা করে, কারণ তারা বাফারিংয়ের মাধ্যমে ডেটা প্রক্রিয়া করে।
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import java.io.File;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
// Create a File object pointing to a file
File file = new File("example.txt");
try {
// Using LineIterator (BufferedReader equivalent) to read file line by line
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
// Iterate over the file lines and print each line
while (it.hasNext()) {
System.out.println(it.nextLine());
}
// Close the iterator to free resources
LineIterator.closeQuietly(it);
} catch (IOException e) {
e.printStackTrace();
}
}
}
এখানে:
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
// Create a File object for the destination file
File file = new File("output.txt");
try {
// Using FileUtils.writeStringToFile() to write a string to a file
String content = "This is an example text for BufferedWriter example.";
FileUtils.writeStringToFile(file, content, "UTF-8");
System.out.println("Text has been written to the file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
এখানে:
FileReader এবং FileWriter Java এর মূল I/O ক্লাস, যা Character-based stream এর মাধ্যমে ফাইল রিড এবং রাইট করতে ব্যবহৃত হয়। তবে Apache Commons IO লাইব্রেরির FileUtils এবং অন্যান্য ইউটিলিটি ক্লাস ব্যবহার করে আমরা আরও সহজে এবং কার্যকরভাবে ফাইলের সাথে কাজ করতে পারি।
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) {
// Create a File object for the file to be read
File file = new File("example.txt");
try {
// Using FileUtils to read the file content into a String
String content = FileUtils.readFileToString(file, "UTF-8");
System.out.println("File content:\n" + content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
এখানে:
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) {
// Create a File object for the destination file
File file = new File("output.txt");
try {
// Using FileUtils to write content to a file
String content = "This is a test content written using FileWriter.";
FileUtils.writeStringToFile(file, content, "UTF-8");
System.out.println("Content written to the file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
এখানে:
FileInputStream এবং FileOutputStream ব্যবহার করে বাইনারি ফাইল রিড এবং রাইট করা যায়। Apache Commons IO লাইব্রেরি এই স্ট্রিমগুলির উপর আরও সহজ ইউটিলিটি ক্লাস সরবরাহ করে যা ফাইল ম্যানিপুলেশনকে আরও সহজ করে তোলে।
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class FileInputStreamExample {
public static void main(String[] args) {
// Create a File object for the file to be read
File file = new File("example.txt");
try {
// Read content of the file using FileUtils (InputStream)
byte[] content = FileUtils.readFileToByteArray(file);
System.out.println("File content read in byte format:");
for (byte b : content) {
System.out.print((char) b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
এখানে:
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class FileOutputStreamExample {
public static void main(String[] args) {
// Create a File object for the destination file
File file = new File("output.txt");
try {
// Write byte content to file using FileUtils (OutputStream)
byte[] content = "This is a binary file content example.".getBytes();
FileUtils.writeByteArrayToFile(file, content);
System.out.println("Binary content written to file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
এখানে:
BufferedReader এবং BufferedWriter ব্যবহার করে Buffered I/O এর মাধ্যমে ফাইল রিড এবং রাইট করা হয়, যা পারফরম্যান্সে উল্লেখযোগ্য উন্নতি আনে। স্ট্রিমের মাধ্যমে ডেটা একবারে ব্লক আকারে পড়া এবং লেখা হয়, যা ছোট ছোট রিড/রাইট অপারেশনের তুলনায় অনেক দ্রুত।
import java.io.*;
public class BufferedReaderWriterExample {
public static void main(String[] args) throws IOException {
// Create a BufferedReader to read the file efficiently
BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
String line;
// Create a BufferedWriter to write the output file efficiently
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
// Read and write line by line
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine(); // Add a new line after each line of text
}
// Close the streams
reader.close();
writer.close();
System.out.println("File copied using BufferedReader and BufferedWriter.");
}
}
এখানে:
Apache Commons IO লাইব্রেরি ব্যবহার করে Reader এবং Writer ব্যবস্থাপনা অনেক সহজ হয়ে যায়। BufferedReader এবং BufferedWriter এর মাধ্যমে ফাইল পড়া এবং লেখা দ্রুত করা যায়, FileUtils এবং IOUtils এর মতো ইউটিলিটি ক্লাস ব্যবহার করে আপনি সহজেই ফাইলের কন্টেন্ট রিড/রাইট এবং কপি করতে পারেন। Java এর বিল্ট-ইন I/O ক্লাসগুলির কার্যক্ষমতা বাড়াতে Apache Commons IO অত্যন্ত সহায়ক, বিশেষত যখন বড় বা বাইনারি ফাইলের সাথে কাজ করতে হয়।
common.read_more